package internal

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
	"io"
	"os"
)

//初始化viper
func InitConfig() {

	viper.SetConfigName("configs/config")
	viper.AddConfigPath(".")
	err := viper.ReadInConfig()
	if err != nil {
		panic(fmt.Errorf("Fatal error config file: %s \n", err))
	}

}

// InitFile 初始化日志
func InitFile() {
	// Disable Console Color, you don't need console color when writing the logs to file.
	gin.DisableConsoleColor()
	var f *os.File
	// Logging to a file.
	logFile := viper.GetString("app.logFile")

	if !FileExist(logFile) {
		f, _ = os.Create(logFile)

	} else {
		f, _ = os.Open(logFile)

	}

	//defer f.Close()
	gin.DefaultWriter = io.MultiWriter(f)

}

// FileExist 文件判断，借鉴大佬
func FileExist(path string) bool {
	_, err := os.Lstat(path)
	return !os.IsNotExist(err)
}
